<?php
/**
 * 管理员
 */
include LIB_PATH . '/YAction.class.php';
class AdminUserAction extends YAction {
	public function __construct() {
		parent::__construct();
		
		// 登录检测
		if(!$this->isLogin()) {
			echo '_error Access Timeout'; exit;
		}
	}
	
	// 列表
	public function index() {
		// 用户列表
		$data = D('Admin')->getListByPage();
		$pageString = $data['pageString'];
		unset($data['pageString']);
		$this->assign('data', $data);
		$this->assign('pageString', $pageString);
		
		$this->display();
	}
	
	// 添加
	public function addAdmin() {
		
		$this->display();
	}
	public function doAddAdmin() {
		$data = array();
		$data['username'] = y_getParam('username');
		$data['password'] = y_getParam('password');
		$confirming = y_getParam('confirming');
		$data['email'] = y_getParam('email');
		$data['desc'] = y_getParam('desc');
		
		if(empty($data['username'])) {
			$this->error('请输入用户名');
			exit;
		}
		if(empty($data['password'])) {
			$this->error('请输入密码');
			exit;
		}
		if($confirming != $data['password']) {
			$this->error('密码不一致');
			exit;
		}
		
		$data['password'] = md5($data['password']);
		
		D('Admin')->addOne($data);
		
		$this->success('添加成功');
	}
	
	// 编辑
	public function editAdmin() {
		$id = y_getParam('id', 'get');
		if(!is_numeric($id)) {exit;}
		
		$data = D('Admin')->getOneByPk($id);
		$this->assign('data', $data);
		
		$this->display();
	}
	public function doEditAdmin() {
		$data = array();
		$id = y_getParam('pkid');
		if(!is_numeric($id)) {exit;}
		
		$data['username'] = y_getParam('username');
		$password = y_getParam('password');
		if(!empty($password) /*&& $password == y_getParam('confirming')*/ ) {
			$data['password'] = md5($password);
		}
		$data['email'] = y_getParam('email');
		$data['desc'] = y_getParam('desc');
		
		D('Admin')->updateOne('id='.$id, $data);
		
		$this->success('修改成功');
	}
	
	// 删除
	public function delAdmin() {
		$id = y_getParam('id', 'get');
		if(!is_numeric($id)) {exit;}
		
		D('Admin')->deleteOne('id='.$id);
		
		$this->success('删除成功');
	}
}